﻿@using SmartStore.Core.Domain.Forums;
@using SmartStore.Web.Models.Boards;
@using SmartStore.Web.Models.Common;
@using SmartStore.Web.Framework.UI;
@model ForumPageModel
@{
    Layout = "_Layout";

    Html.AddTitleParts(Model.Name);
    Html.AddMetaDescriptionParts(Model.Name);

    var pager = Html.SmartStore().Pager(Model.TopicPageIndex, Model.TopicPageSize, Model.TopicTotalRecords)
        .Name("paginator")
        .QueryParam("page")
        .ShowSummary(false)
        .Route("ForumSlug", new RouteValues { id = Model.Id, slug = Model.SeName })
        .Alignment(PagerAlignment.Right);
}

<script type="text/javascript">
    function handleForumWatch(context) {
        var btn = $('#watch-forum');
        btn.text(context.Text);
        setButtonWatchState(btn, context.Subscribed);
    }
    function setButtonWatchState(btn, subscribed) {
        subscribed = (subscribed !== undefined) ? subscribed : toBool(btn.data("subscribed"));
        btn.find("i").remove();
        btn.prepend('<i class="fa fa-bookmark" />&nbsp;').toggleClass("btn-outline-warning", subscribed);
    }
    $(function () {
        setButtonWatchState($('#watch-forum'));
    });
</script>

<div class="page forum">
    <div class="mt-2 w-75">
        @{ Html.RenderAction("SearchBox", "Boards"); }
    </div>
    <div class="row mt-5">
        <div class="col">
            <div class="heading mb-4">
                <h2 class="heading-title" @Html.LanguageAttributes(Model.Name)>@Model.Name</h2>
            </div>
        </div>
        <div class="col col-auto">
            @if (Model.ForumFeedsEnabled)
            {
                <a href="@Url.Action("ForumRSS", new { id = Model.Id })" class="btn btn-warning" title="@T("Forum.ForumRSSLinkTitle")">
                    <i class="fa fa-rss"></i>
                    <span>@T("Forum.RSS")</span>
                </a>
            }
        </div>
    </div>

    <p class="lead" @Html.LanguageAttributes(Model.Description)>@Model.Description</p>

    <div>
        <div class="actions my-3 row sm-gutters">
            <div class="col col-sm-auto mt-1 mt-sm-0">
                <a href="@Url.Action("TopicCreate", new { id = Model.Id })" class="btn btn-primary newtopic btn-block" rel="nofollow">
                    <i class="fa fa-plus"></i>
                    <span>@T("Forum.NewTopic")</span>
                </a>
            </div>
            <div class="col col-sm-auto mt-1 mt-sm-0">
                @if (Model.IsCustomerAllowedToSubscribe)
                {
                    @Ajax.ActionLink(Model.WatchForumText,
                            "ForumWatch",
                            new { id = @Model.Id },
                            new AjaxOptions { HttpMethod = "Post", OnSuccess = "handleForumWatch" },
                            new { id = "watch-forum", @class = "btn btn-outline-primary font-weight-bold btn-block watch-forum", data_subscribed = Model.WatchForumSubscribed })
                }
            </div>
        </div>
        @pager
    </div>

    <div class="topics-group table-responsive">
        <table class="table">
            @if (Model.ForumTopics.Count > 0)
            {
                <thead>
                    <tr class="forum-header">
                        <th></th>
                        <th class="topic-name">
                            @T("Forum.TopicTitle")
                        </th>
                        <th class="replies d-none d-md-table-cell">
                            @T("Forum.Replies")
                        </th>
                        <th class="views d-none d-md-table-cell">
                            @T("Forum.Views")
                        </th>
                        <th class="last-post d-none d-md-table-cell">
                            @T("Forum.LatestPost")
                        </th>
                    </tr>
                </thead>
            }
            @foreach (var topic in Model.ForumTopics)
            {
                <tr class="topic@(topic.Published ? "" : " disabled")">
                    <td class="image">
                        @{ Html.RenderPartial("Customer.Avatar", topic.Avatar); }
                    </td>
                    <td class="topic-name">
                        @if (topic.ForumTopicType == ForumTopicType.Sticky)
                        {
                            <span class="topictype"><label class="badge badge-success">@T("Forum.Sticky")</label></span>
                        }
                        else if (topic.ForumTopicType == ForumTopicType.Announcement)
                        {
                            <span class="topictype"><label class="badge badge-info">@T("Forum.Announcement")</label></span>
                        }
                        @Html.RouteLink(topic.Subject, "TopicSlug", new { id = topic.Id, slug = topic.SeName }, new { @class = "topic-title" })                        
                        <div>
                            <div class="topic-starter text-muted d-inline-flex">
                                @if (topic.CustomerId > 0)
                                {
									if (topic.AllowViewingProfiles && !topic.IsCustomerGuest)
									{
										<span class="pr-1">@T("Forum.Author"):</span>
										<span>@Html.RouteLink(topic.CustomerName, "CustomerProfile", new { Id = topic.CustomerId })</span>
									}
									else
									{
										<span class="pr-1">@T("Forum.Author"):</span>
										<span>@topic.CustomerName</span>
									}
								}
                            </div>
                            @if (topic.NumPosts > Model.PostsPageSize)
                            {
                                @(Html.SmartStore().Pager(topic.TotalPostPages)
                                    .QueryParam("page")
                                    .HtmlAttributes(new { @class = "topics-pager" })
                                    .WithRenderer<ForumTopicPagerRenderer>()
                                    .ShowPrevious(false)
                                    .ShowNext(false)
                                    .ItemTitleFormatString(T("Forum.Topics.GotoPostPager"))
                                    .Alignment(PagerAlignment.Left)
                                    .Size(PagerSize.Small)
                                    .Route("TopicSlug", new { id = topic.Id, slug = topic.SeName }))
                            }
                        </div>
                    </td>
                    <td class="replies d-none d-md-table-cell">
                        @topic.NumReplies.ToString("N0")
                    </td>
                    <td class="views d-none d-md-table-cell">
                        @topic.Views.ToString("N0")
                    </td>
                    <td class="last-post d-none d-md-table-cell">
                        @{ Html.RenderPartial("LastPost", topic.LastPost); }
                    </td>
                </tr>
            }
        </table>
    </div>

    @pager
</div>
